﻿#include "Measure_Rect.h"
#include "qdebug.h"
Measure_Rect::Measure_Rect()
{

	/*CameraParameters.Clear();
	CameraParameters[0] = 0;
	CameraParameters[1] = 0.729467;
	CameraParameters[2] = 1.1494e-005;
	CameraParameters[3] = 1.14995e-005;
	CameraParameters[4] = 1000.23;
	CameraParameters[5] = 750.148;
	CameraParameters[6] = 2000;
	CameraParameters[7] = 1500;*/

	CameraParameters.Clear();
	CameraParameters[0] = 0;
	CameraParameters[1] = -0.940741;
	CameraParameters[2] = 1.14751e-005;
	CameraParameters[3] = 1.14709e-005;
	CameraParameters[4] = 891.283;
	CameraParameters[5] = 943.919;
	CameraParameters[6] = 2000;
	CameraParameters[7] = 1500;

//CameraParameters: = [0, 0.729467, 1.1494e-005, 1.14995e-005, 1000.23, 750.148, 2000, 1500]
	//hv_CameraParameters[0] = 0;
	//hv_CameraParameters[1] = -1.32651;
	//hv_CameraParameters[2] = 1.14473e-005;
	//hv_CameraParameters[3] = 1.14931e-005;
	//hv_CameraParameters[4] = 997.184;
	//hv_CameraParameters[5] = 751.793;
	//hv_CameraParameters[6] = 2000;
	//hv_CameraParameters[7] = 1500;
	/*CameraParameters[0] = 0;
	CameraParameters[1] = -0.940741;
	CameraParameters[2] = 1.14751e-005;
	CameraParameters[3] = 1.14709e-005;
	CameraParameters[4] = 891.283;
	CameraParameters[5] = 943.919;
	CameraParameters[6] = 2000;
	CameraParameters[7] = 1500;*/
	//GenRadialDistortionMap(&Map, CameraParameters, CameraParameters, "bilinear");
}
int Measure_Rect::M_Rect(HObject Img,int M_Model,HTuple &OutPara,HObject &OutImg)
{
    int flag=0;
    OutPara.Clear();


    HObject  Region, Rectangle1, ImageReduced1, Region1,Rect,Contour;
    HObject  ImageResult1, Edges1, ERegion1, EndPoints1;
    HObject  JuncPoints1, ConnectedRegions1;
    HObject  Rectangle2;
    HObject  ImageReduced2, Region2, ImageResult2, Edges2;
    HObject  ERegion2, EndPoints2, JuncPoints2, ConnectedRegions2;
    HObject  ImageMapped, RegionTrans;
	HObject ImageRotate, Region3;

    HTuple  Row, Column, Phi, W, H, r1, c1;
    HTuple  line1, line2, r2, c2, line3, line4;
    HTuple  Index1, Line1;
    HTuple  Line2;
    HTuple  Line3;
    HTuple  Line4, Distance1, Distance2, Distance3;
    HTuple  Distance4, Len1, Len2, Angle1, Angle2;
    HTuple  OutPara1,OutPara2,MidPhi;
    HTuple  A1, A2, A3, A4, Number;
	HTuple MetrologyHandle1, MetrologyHandle2, MetrologyHandle3, MetrologyHandle4;
	HTuple Row1, Column1, Row2, Column2;

    Flag1=true;
    Flag2=true;
    Flag3=true;
    Flag4=true;
    Flag5=true;
    Flag6=true;
    Flag7=true;
    Flag8=true;


	Flag11 = true;

	Flag33 = true;

	Flag55 = true;
	Flag66 = true;
	Flag77 = true;
	Flag88 = true;

    float Angle=0.05;
    /*
    //换算比值
    float para1=0.011468;
    //补偿值
    float para2=0.008;
    //——————————————————————垂直方向
    //补偿值
    float para3 = 0;
    //标准值w
    float para4 = 2.18;
    //上限值
    float para5 = 0.01;
    //下限值
    float para6 = 0.01;
    //平行度补偿值
    float para7 = 0;
    //平行度标准值
    float para8 = 0;
    //平行度偏差值
    float para9 = 1;


    //——————————————————————水平方向
    //补偿值
    float para10 = 0;
    //标准值
    float para11 = 6.00;
    //上限值
    float para12 = 0.01;
    //下限值
    float para13 = 0.01;
    //平行度补偿值
    float para14 = 0;
    //平行度标准值
    float para15 = 0;
    //平行度偏差值
    float para16 = 1;

    //------------------图像处理

    //粗定定位阈值
    float para17=60;
    //找边向外沿伸长度
    float para18=5;
    //找边sigma
    float para19=3;
    //找边阈值
    float para20=60;

    //角度1补偿值
    float para21;
    //角度1标准值
    float para22;
    //角度1上限值
    float para23;
    //角度1下限值
    float para24;


    //角度2补偿值
    float para25;
    //角度2标准值
    float para26;
    //角度2上限值
    float para27;
    //角度2下限值
    float para28;


    //角度3补偿值
    float para29;
    //角度3标准值
    float para30;
    //角度3上限值
    float para31;
    //角度3下限值
    float para32;


    //角度4补偿值
    float para33;
    //角度4标准值
    float para34;
    //角度4上限值
    float para35;
    //角度4下限值
    float para36;
*/

    //换算比例
    float para1 = m_part.para1;
    //总体补偿
    float para2 = m_part.para2;


    //——————————————————————垂直方向
    //补偿值
    float para3 = m_part.para3;
    //标准值
    float para4 = m_part.para4;
    //上限值
    float para5 = m_part.para5;
    //下限值
    float para6 = m_part.para6;
    //平行度补偿值
    float para7 = m_part.para7;
    //平行度标准值
    float para8 = m_part.para8;
    //平行度偏差值
    float para9 = m_part.para9;


    //——————————————————————水平方向
    //补偿值
    float para10 = m_part.para10;
    //标准值
    float para11 = m_part.para11;
    //上限值
    float para12 = m_part.para12;
    //下限值
    float para13 = m_part.para13;
    //平行度补偿值
    float para14 = m_part.para14;
    //平行度标准值
    float para15 = m_part.para15;
    //平行度偏差值
    float para16 = m_part.para16;

    //------------------图像处理

    //粗定定位阈值
    float para17 = m_part.para17;
    //找边向外沿伸长度
    float para18 = m_part.para18;
    //找边sigma
    float para19 = m_part.para19;
    //找边阈值
    float para20 = m_part.para20;


    //角度1补偿值
    float para21 = m_part.para21;
    //角度1标准值
    float para22 = m_part.para22;
    //角度1上限值
    float para23 = m_part.para23;
    //角度1下限值
    float para24 = m_part.para24;


    //角度2补偿值
    float para25 = m_part.para25;
    //角度2标准值
    float para26 = m_part.para26;
    //角度2上限值
    float para27 = m_part.para27;
    //角度2下限值
    float para28 = m_part.para28;


    //角度3补偿值
    float para29 = m_part.para29;
    //角度3标准值
    float para30 = m_part.para30;
    //角度3上限值
    float para31 = m_part.para31;
    //角度3下限值
    float para32 = m_part.para32;


    //角度4补偿值
    float para33 = m_part.para33;
    //角度4标准值
    float para34 = m_part.para34;
    //角度4上限值
    float para35 = m_part.para35;
    //角度4下限值
    float para36 = m_part.para36;

    try
    {

        OutImg=Img;
        GenRectangle1(&Rect, m_part2.x1, m_part2.y1, m_part2.x2, m_part2.y2);
		double aa = m_part2.x1;
		double bb = m_part2.y1;
		double cc = m_part2.x2;
		double dd = m_part2.y2;
        GetImageSize(Img, &W, &H);
        if ((W != 2000) || (H != 1500))
        {
            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
            Flag1 = false;
            Flag2 = false;
            Flag3 = false;
            Flag4 = false;
            Flag5 = false;
            Flag6 = false;
            Flag7 = false;
            Flag8 = false;
            flag = 2;
            return flag;
        }

     /*   CameraParameters.Clear();
        CameraParameters[0] = 0;
        CameraParameters[1] = -0.940741;
        CameraParameters[2] = 1.14751e-005;
        CameraParameters[3] = 1.14709e-005;
        CameraParameters[4] = 891.283;
        CameraParameters[5] = 943.919;
        CameraParameters[6] = 2000;
        CameraParameters[7] = 1500;
        GenRadialDistortionMap(&Map, CameraParameters, CameraParameters, "bilinear");*/


        MapImage(Img, Map, &ImageMapped);

        ReduceDomain(ImageMapped,Rect,&ImageMapped);

        Threshold(ImageMapped, &Region, 0, para17);
        Connection(Region, &Region);
        SelectShape(Region, &Region, "area", "and", 10000, 9999999);
        CountObj(Region, &Number);
        if(!(Number.D() == 1))
        {
            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
            Flag1 = false;
            flag = -1;
            return flag;
        }
        Number=0;
//        SelectShapeStd(Region, &Region, "max_area", 70);
        //**bengbian
        HObject RegionTrans11,Region11,ImageReduced11,ConnectedRegions11;
        HTuple Num11;
        Num11=0;
        ShapeTrans(Region, &RegionTrans11, "rectangle2");
        ReduceDomain (ImageMapped, RegionTrans11, &ImageReduced11);
        Threshold (ImageReduced11, &Region11, 128, 255);
        Connection (Region11, &ConnectedRegions11);
        SelectShape(ConnectedRegions11, &ConnectedRegions11, "area", "and", 1000, 9999999);
        CountObj(ConnectedRegions11, &Num11);
        if(Num11!=0)
        {

            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
            Flag1 = false;
            flag = 2;
            return flag;
        }

        ShapeTrans(Region, &RegionTrans, "convex");
        Difference(RegionTrans, Region, &RegionTrans);
        ErosionRectangle1(RegionTrans, &RegionTrans, 11, 11);
        Connection(RegionTrans, &RegionTrans);
		
        SelectShape(RegionTrans, &RegionTrans, "area", "and", 30, 9999999);
        CountObj(RegionTrans, &Number);



        if (!(Number.D() == 0))
        {
            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
            Flag1 = false;
            flag = 2;
            return flag;
        }


        SmallestRectangle2(Region, &Row, &Column, &Phi, &W, &H);

		RotateImage(ImageMapped, &ImageRotate, Phi, "constant");
		ReduceDomain(ImageRotate, Rect, &ImageRotate);
		Threshold(ImageRotate, &Region3, 0, para17);
		Connection(Region3, &Region3);
		SelectShapeStd(Region3, &Region3, "max_area", 70);
		SmallestRectangle1(Region3, &Row1, &Column1, &Row2, &Column2);
	    W = (Column2 - Column1) / 2;
		H = (Row2 - Row1) / 2;





        MidPhi = Phi;
        if((Phi>PI/2)&&(Phi<PI/2))
        {
            Phi=Phi-PI/2;
        }
        else if((-PI/4<Phi)&&(Phi<0))
        {
            Phi=Phi+PI;
        }
        else if((Phi>-1)&&(Phi<PI/4))
        {
            Phi=Phi;
        }
        else
        {
            Phi=Phi+PI/2;
        }
        //测试垂直高度
        if((M_Model==1)||(M_Model==0))
        {
            GenRectangle2(&Rectangle1, Row, Column, Phi, W/2, H+60);
            ReduceDomain(ImageMapped, Rectangle1, &ImageReduced1);
            Threshold(ImageReduced1, &Region1, 0, para17);
            Connection (Region1, &Region1);
            SelectShapeStd(Region1, &Region1, "max_area", 70);
            DilationRectangle1(Region1, &Region1, para18, para18);
            ReduceDomain(ImageMapped, Region1, &ImageResult1);
            EdgesSubPix(ImageResult1, &Edges1, "canny", 3, 20, 40);
            SelectContoursXld(Edges1, &Edges1, "contour_length", 100, 20000, -0.5, 0.5);
            UnionAdjacentContoursXld(Edges1, &Edges1, 10, 1, "attr_keep");
            GenRegionContourXld(Edges1, &ERegion1, "filled");

            SelectShape(ERegion1, &ERegion1, "width", "and", W - 30, 2 * W + 30);
            JunctionsSkeleton(ERegion1, &EndPoints1, &JuncPoints1);
            Connection(EndPoints1, &ConnectedRegions1);
            RegionFeatures(ConnectedRegions1, "row", &r1);
            RegionFeatures(ConnectedRegions1, "column", &c1);
            line1.Clear();
            line1.Append(HTuple(r1[0])).Append(HTuple(c1[0])).Append(HTuple(r1[1])).Append(HTuple(c1[1]));
            line2.Clear();
            line2.Append(HTuple(r1[2])).Append(HTuple(c1[2])).Append(HTuple(r1[3])).Append(HTuple(c1[3]));


            //线1
            CreateMetrologyModel(&MetrologyHandle1);
            AddMetrologyObjectLineMeasure(MetrologyHandle1, HTuple(line1[0]), HTuple(line1[1]),
                    HTuple(line1[2]), HTuple(line1[3]), 20, 5, para19, para17, HTuple(),
                    HTuple(), &Index1);
            ApplyMetrologyModel(ImageMapped, MetrologyHandle1);
            GetMetrologyObjectResult(MetrologyHandle1, "all", "all", "result_type",
                                     "all_param", &Line1);
            ClearMetrologyModel(MetrologyHandle1);
            //线2
            CreateMetrologyModel(&MetrologyHandle2);
            AddMetrologyObjectLineMeasure(MetrologyHandle2, HTuple(line2[0]), HTuple(line2[1]),
                    HTuple(line2[2]), HTuple(line2[3]), 20, 5, para19, para17, HTuple(),
                    HTuple(), &Index1);
            ApplyMetrologyModel(ImageMapped, MetrologyHandle2);
            GetMetrologyObjectResult(MetrologyHandle2, "all", "all", "result_type",
                                     "all_param", &Line2);
            ClearMetrologyModel(MetrologyHandle2);

            DistancePl(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line2[0]), HTuple(Line2[1]),
                    HTuple(Line2[2]), HTuple(Line2[3]), &Distance1);
            DistancePl(HTuple(Line1[2]), HTuple(Line1[3]), HTuple(Line2[0]), HTuple(Line2[1]),
                    HTuple(Line2[2]), HTuple(Line2[3]), &Distance2);

            //（高度）
            Len1 = (Distance1+Distance2)/2;
			/*double bb1 = Len1.D();
            Len1 = Len1*para1+para2+para3;
			double bb = Len1.D();*/
            //短边
           /* if(!(((Len1)>(para4-para6))&&((Len1)<(para4+para5))))
            {
                Flag1=false;
                flag=1;
            }*/
            //平行度
            AngleLl(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line1[2]), HTuple(Line1[3]),
                    HTuple(Line2[0]), HTuple(Line2[1]), HTuple(Line2[2]), HTuple(Line2[3]),
                    &Angle1);
            Angle1 = ((Angle1*180)/PI).TupleAbs()+para7;
            if(!(Angle1<(para8+para9)))
            {
				
                Flag2=false;
                flag=1;
            }
            //TupleConcat (Len1, Angle1, &OutPara1);
        }
        if((Phi<PI/2)||(Phi>PI*3/4))
        {
            Phi=Phi+PI/2;
        }

        else
        {
            Phi=Phi;
        }

        // 测试水平长度
        if((M_Model==2)||(M_Model==0))
        {
            GenRectangle2(&Rectangle2, Row, Column, Phi, H/2,W+60);
            ReduceDomain(ImageMapped, Rectangle2, &ImageReduced2);
            Threshold(ImageReduced2, &Region2, 0, para17);
            SelectShapeStd(Region2, &Region2, "max_area", 70);
            DilationRectangle1(Region2, &Region2, para18, para18);
            ReduceDomain(ImageMapped, Region2, &ImageResult2);

            EdgesSubPix(ImageResult2, &Edges2, "canny", 3, 20, 40);

            SelectContoursXld(Edges2, &Edges2, "contour_length", 100, 20000, -0.5, 0.5);
            UnionAdjacentContoursXld(Edges2, &Edges2, 10, 1, "attr_keep");
            GenRegionContourXld(Edges2, &ERegion2, "filled");
            SelectShape(ERegion2, &ERegion2, "height", "and", H - 30, H + 30);
            JunctionsSkeleton(ERegion2, &EndPoints2, &JuncPoints2);
            Connection(EndPoints2, &ConnectedRegions2);
            RegionFeatures(ConnectedRegions2, "row", &r2);
            RegionFeatures(ConnectedRegions2, "column", &c2);

            line3.Clear();
            line3.Append(HTuple(r2[0])).Append(HTuple(c2[0])).Append(HTuple(r2[1])).Append(HTuple(c2[1]));
            line4.Clear();
            line4.Append(HTuple(r2[2])).Append(HTuple(c2[2])).Append(HTuple(r2[3])).Append(HTuple(c2[3]));
            //线3
            CreateMetrologyModel(&MetrologyHandle3);

            AddMetrologyObjectLineMeasure(MetrologyHandle3, HTuple(line3[0]), HTuple(line3[1]),
                    HTuple(line3[2]), HTuple(line3[3]), 20, 5, para19, para17, HTuple(),
                    HTuple(), &Index1);
            ApplyMetrologyModel(ImageMapped, MetrologyHandle3);
            GetMetrologyObjectResult(MetrologyHandle3, "all", "all", "result_type",
                                     "all_param", &Line3);
            ClearMetrologyModel(MetrologyHandle3);
            //线4
            CreateMetrologyModel(&MetrologyHandle4);
            AddMetrologyObjectLineMeasure(MetrologyHandle4, HTuple(line4[0]), HTuple(line4[1]),
                    HTuple(line4[2]), HTuple(line4[3]), 20, 5, para19, para17, HTuple(),
                    HTuple(), &Index1);
            ApplyMetrologyModel(ImageMapped, MetrologyHandle4);
            GetMetrologyObjectResult(MetrologyHandle4, "all", "all", "result_type",
                                     "all_param", &Line4);
            ClearMetrologyModel(MetrologyHandle4);

            //计算边长

            DistancePl(HTuple(Line3[0]), HTuple(Line3[1]), HTuple(Line4[0]), HTuple(Line4[1]),
                    HTuple(Line4[2]), HTuple(Line4[3]), &Distance3);
            DistancePl(HTuple(Line3[2]), HTuple(Line3[3]), HTuple(Line4[0]), HTuple(Line4[1]),
                    HTuple(Line4[2]), HTuple(Line4[3]), &Distance4);

            //长边
            Len2 = (Distance3+Distance4)/2;
			/*double aa= Len2.D();
            Len2 = Len2*para1+para2+para10;
			double aa1 = Len2.D();*/
            //长边
           /* if(!(((Len2)>(para11-para13))&&((Len2)<(para11+para12))))
            {
                Flag3=false;
                flag=1;
            }*/
            AngleLl(HTuple(Line3[0]), HTuple(Line3[1]), HTuple(Line3[2]), HTuple(Line3[3]),
                    HTuple(Line4[0]), HTuple(Line4[1]), HTuple(Line4[2]), HTuple(Line4[3]),
                    &Angle2);
            Angle2 = ((Angle2*180)/PI).TupleAbs()+para14;
            if(!(Angle2<(para15+para16)))
            {
                Flag4=false;
                flag=1;
            }
            //TupleConcat (Len2, Angle2, &OutPara2);
        }

        if (Len1 <= Len2)
        {
            Len1 = Len1;
            Len2 = Len2;
        }
        else
        {
            double MidLen = 0;
            MidLen = Len1;
            Len1 = Len2;
            Len2 = MidLen;
        }

        Len1 = Len1*para1 + para2 + para3+0.01;
//		Len2 = Len2*para1 + para2 + para10+0.014;
        Len2 = Len2*para1 + para2 + para10+0.012;
        if (Len2 >= 10)
        {
            Len2 = Len2 + 0.011+0.002;
        }

        if (M_Model == 0)
        {
            if (MidPhi > 0)
            {
                AngleLl(Line1[0], Line1[1], Line1[2], Line1[3], Line3[2], Line3[3], Line3[0], Line3[1], &A1);
                AngleLl(Line1[0], Line1[1], Line1[2], Line1[3], Line4[0], Line4[1], Line4[2], Line4[3], &A2);
                AngleLl(Line2[0], Line2[1], Line2[2], Line2[3], Line4[2], Line4[3], Line4[0], Line4[1], &A3);
                AngleLl(Line2[0], Line2[1], Line2[2], Line2[3], Line3[0], Line3[1], Line3[2], Line3[3], &A4);
            }
            else
            {
                AngleLl(Line1[0], Line1[1], Line1[2], Line1[3], Line3[0], Line3[1], Line3[2], Line3[3], &A1);
                AngleLl(Line1[2], Line1[3], Line1[0], Line1[1], Line4[0], Line4[1], Line4[2], Line4[3], &A2);
                AngleLl(Line2[2], Line2[3], Line2[0], Line2[1], Line4[2], Line4[3], Line4[0], Line4[1], &A3);
                AngleLl(Line2[0], Line2[1], Line2[2], Line2[3], Line3[2], Line3[3], Line3[0], Line3[1], &A4);
            }
            A1 = A1 * 180 / PI;
            A2 = A2 * 180 / PI;
            A3 = A3 * 180 / PI;
            A4 = A4 * 180 / PI;
            TupleAbs(A1, &A1);
            TupleAbs(A2, &A2);
            TupleAbs(A3, &A3);
            TupleAbs(A4, &A4);
            A1 = A1 + para21;
            A2 = A2 + para25;
            A3 = A3 + para29;
            A4 = A4 + para33;
            if (!((A1<(para22 + para23-Angle)) && (A1>(para22 - para24+Angle))))
            {
				if (A1 < (para22 - para24))
				{
					Flag55 = false;
				}
                Flag5 = false;
                flag = 1;
            }
            if (!((A2<(para26 + para27-Angle)) && (A2>(para26 - para28+Angle))))
            {
				if (A2 < (para26 - para28))
				{
					Flag66 = false;
				}
                Flag6 = false;
                flag = 1;
            }
            if (!((A3<(para30 + para31-Angle)) && (A3>(para30 - para32+Angle))))
            {
				if (A3 < (para30 - para32))
				{
					Flag77 = false;
				}
                Flag7 = false;
                flag = 1;
            }
            if (!((A4<(para34 + para35-Angle)) && (A4>(para34 - para36+Angle))))
            {
				if (A4<(para34 - para36))
				{
					Flag88= false;
				}
                Flag8 = false;
                flag = 1;
            }

            //短边
            if((Len1>(para4-0.1))&&(Len1<(para4+0.1))&&(Len2>(para11 - 0.1))&&(Len2 < (para11 + 0.1)))
            {
                if (!(((Len1) > (para4 - para6)) && ((Len1)<(para4 + para5))))
                {
                    if ((Len1)<(para4 - para6))
                    {
                        Flag11 = false;
                    }
                    Flag1 = false;
                    flag = 1;
                }
                //长边
                if (!(((Len2)>(para11 - para13)) && ((Len2) < (para11 + para12))))
                {
                    if ((Len2) < (para11 - para13))
                    {
                        Flag33 = false;
                    }
                    Flag3 = false;
                    flag = 1;
                }

            }
            else
            {

                Flag1=false;
                Flag3=false;
                flag=-1;
            }

            TupleConcat(Len1, Angle1, &OutPara1);
            TupleConcat(Len2, Angle2, &OutPara2);




            if(M_Model==1)
            {
                OutPara=OutPara1;
            }
            else if(M_Model==2)
            {
                OutPara=OutPara2;
            }
            else
            {
                TupleConcat (OutPara1, OutPara2, &OutPara2);
                OutPara=OutPara2;
            }
            TupleConcat(OutPara, A1, &OutPara);
            TupleConcat(OutPara, A2, &OutPara);
            TupleConcat(OutPara, A3, &OutPara);
            TupleConcat(OutPara, A4, &OutPara);
        }


		/*
        //*如果判断错误

        if(flag==1)
        {
            flag=0;
            Flag1=true;
            Flag2=true;
            Flag3=true;
            Flag4=true;
            Flag5=true;
            Flag6=true;
            Flag7=true;
            Flag8=true;
            OutPara1.Clear();
            OutPara2.Clear();
            //短边
            if(!(((Len2)>(para4-para6))&&((Len2)<(para4+para5))))
            {
                Flag1=false;
                flag=1;
            }
            //平行度
            AngleLl(HTuple(Line1[0]), HTuple(Line1[1]), HTuple(Line1[2]), HTuple(Line1[3]),
                    HTuple(Line2[0]), HTuple(Line2[1]), HTuple(Line2[2]), HTuple(Line2[3]),
                    &Angle2);
            Angle2 = ((Angle2*180)/PI).TupleAbs()+para7;
            if(!(Angle2<(para8+para9)))
            {
                Flag2=false;
                flag=1;
            }
            TupleConcat (Len2, Angle2, &OutPara2);



            //长边
            if(!(((Len1)>(para11-para13))&&((Len1)<(para11+para12))))
            {
                Flag3=false;
                flag=1;
            }
            AngleLl(HTuple(Line3[0]), HTuple(Line3[1]), HTuple(Line3[2]), HTuple(Line3[3]),
                    HTuple(Line4[0]), HTuple(Line4[1]), HTuple(Line4[2]), HTuple(Line4[3]),
                    &Angle1);
            Angle1 = ((Angle1*180)/PI).TupleAbs()+para14;
            if(!(Angle1<(para15+para16)))
            {
                Flag4=false;
                flag=1;
            }
            TupleConcat (Len1, Angle1, &OutPara1);




            if(M_Model==1)
            {
                OutPara=OutPara1;
            }
            else if(M_Model==2)
            {
                OutPara=OutPara2;
            }
            else
            {
				TupleConcat( OutPara2, OutPara1,&OutPara2);
                OutPara=OutPara2;
            }
            if (M_Model == 0)
            {
                if (MidPhi > 0)
                {
                    AngleLl(Line1[0], Line1[1], Line1[2], Line1[3], Line3[2], Line3[3], Line3[0], Line3[1], &A1);
                    AngleLl(Line1[0], Line1[1], Line1[2], Line1[3], Line4[0], Line4[1], Line4[2], Line4[3], &A2);
                    AngleLl(Line2[0], Line2[1], Line2[2], Line2[3], Line4[2], Line4[3], Line4[0], Line4[1], &A3);
                    AngleLl(Line2[0], Line2[1], Line2[2], Line2[3], Line3[0], Line3[1], Line3[2], Line3[3], &A4);
                }
                else
                {
                    AngleLl(Line1[0], Line1[1], Line1[2], Line1[3], Line3[0], Line3[1], Line3[2], Line3[3], &A1);
                    AngleLl(Line1[2], Line1[3], Line1[0], Line1[1], Line4[0], Line4[1], Line4[2], Line4[3], &A2);
                    AngleLl(Line2[2], Line2[3], Line2[0], Line2[1], Line4[2], Line4[3], Line4[0], Line4[1], &A3);
                    AngleLl(Line2[0], Line2[1], Line2[2], Line2[3], Line3[2], Line3[3], Line3[0], Line3[1], &A4);
                }
                A1 = A1 * 180 / PI;
                A2 = A2 * 180 / PI;
                A3 = A3 * 180 / PI;
                A4 = A4 * 180 / PI;
                TupleAbs(A1, &A1);
                TupleAbs(A2, &A2);
                TupleAbs(A3, &A3);
                TupleAbs(A4, &A4);
                A1 = A1 + para21;
                A2 = A2 + para25;
                A3 = A3 + para29;
                A4 = A4 + para33;
                if (!((A1<(para22 + para23)) && (A1>(para22 - para24))))
                {
                    Flag5 = false;
                    flag = 1;
                }
                if (!((A2<(para26 + para27)) && (A2>(para26 - para28))))
                {
                    Flag6 = false;
                    flag = 1;
                }
                if (!((A3<(para30 + para31)) && (A3>(para30 - para32))))
                {
                    Flag7 = false;
                    flag = 1;
                }
                if (!((A4<(para34 + para35)) && (A4>(para34 - para36))))
                {
                    Flag8 = false;
                    flag = 1;
                }
                TupleConcat(OutPara, A1, &OutPara);
                TupleConcat(OutPara, A2, &OutPara);
                TupleConcat(OutPara, A3, &OutPara);
                TupleConcat(OutPara, A4, &OutPara);



            }


        }
		*/



        return flag;

    }
    catch(HException &exception)
    {
        flag=2;
        if(M_Model==0)
        {
            OutPara.Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0).Append(0);
        }
        else
        {
            OutPara.Append(0).Append(0);
        }
        Flag1=false;
        Flag2=false;
        Flag3=false;
        Flag4=false;
        Flag5=false;
        Flag6=false;
        Flag7=false;
        Flag8=false;
        return flag;
    }

}
